import type { RouteRecordRaw } from "vue-router"
import { routerConfig } from "@/router/config"
import { registerNavigationGuard } from "@/router/guard"
import { createRouter } from "vue-router"
import { flatMultiLevelRoutes } from "./helper"
import { collectInnerLinkRoutes } from "@/common/utils/permission"

const Layouts = () => import("@/layouts/index.vue")

/**
 * @name 常驻路由
 * @description 除了 redirect/403/404/login 等隐藏页面，其他页面建议设置唯一的 Name 属性
 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layouts,
    meta: {
      hidden: true
    },
    children: [
      {
        path: ":path(.*)",
        component: () => import("@/pages/redirect/index.vue")
      }
    ]
  },
  {
    path: "/info",
    component: Layouts,
    redirect: "/info/index",
    meta: {
      title: "个人资料",
      hidden: true
    },
    children: [
      {
        path: "/info/index",
        name: "InfoIndex",
        component: () => import("@/pages/info/index.vue"),
        meta: {
          title: "个人资料",
          hidden: true
        }
      }
    ]
  },
  {
    path: "/403",
    component: () => import("@/pages/error/403.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/404",
    component: () => import("@/pages/error/404.vue"),
    meta: {
      hidden: true
    },
    alias: "/:pathMatch(.*)*"
  },
  {
    path: "/login",
    component: () => import("@/pages/login/index.vue"),
    meta: {
      hidden: true
    }
  }
]

/**
 * @name 动态路由
 * @description 用来放置有权限 (Roles 属性) 的路由
 * @description 必须带有唯一的 Name 属性
 */
export const dynamicRoutes: RouteRecordRaw[] = [
  {
    path: "/",
    component: Layouts,
    redirect: "/dashboard",
    children: [
      {
        path: "/dashboard",
        name: "Dashboard",
        component: () => import("@/pages/dashboard/index.vue"),
        meta: {
          title: "主页",
          svgIcon: "home" as any,
          affix: true
        }
      }
    ]
  },
  {
    path: "/resource",
    component: Layouts,
    redirect: "/intelligent",
    name: "Resource",
    meta: { title: "资源", svgIcon: "ziyuan" as any, isInnerLink: true },
    children: [
      {
        path: "/intelligent",
        name: "Intelligent",
        redirect: "/intelligent/index",
        meta: { title: "智能体库", svgIcon: "zhinengti" as any },
        children: [
          {
            path: "/intelligent/index",
            name: "IntelligentIndex",
            component: () => import("@/pages/intelligent/index.vue"),
            meta: { title: "智能体库", svgIcon: "zhinengti" as any, keepAlive: true, component: "IntelligentIndex" }
          },
          {
            path: "/intelligent/create",
            name: "IntelligentCreate",
            component: () => import("@/pages/intelligent/create.vue"),
            meta: { title: "创建智能体", svgIcon: "on-model", hidden: true, activeMenu: "/intelligent/index", keepAlive: true, component: "IntelligentCreate" }
          },
          {
            path: "/intelligent/view",
            name: "IntelligentView",
            component: () => import("@/pages/intelligent/view.vue"),
            meta: { title: "查看智能体", svgIcon: "on-model", hidden: true, activeMenu: "/intelligent/index", keepAlive: true, component: "IntelligentView" }
          }
        ]
      },
      {
        path: "/scn",
        redirect: "/scn/index",
        meta: {
          title: "想定库",
          svgIcon: "xiangding" as any,
          module: "test"
        },
        children: [
          {
            path: "/scn/index",
            name: "ScnIndex",
            component: () => import("@/pages/scn/index.vue"),
            meta: { title: "想定库", svgIcon: "xiangding", keepAlive: true, component: "ScnIndex" }
          },
          {
            path: "/scn/view",
            name: "ScnView",
            component: () => import("@/pages/scn/Edit/index.vue"),
            meta: { title: "查看想定", svgIcon: "on-model", hidden: true, activeMenu: "/scn/index", keepAlive: true, component: "ScnView" }
          }
        ]
      },
      {
        path: "/modelProject",
        name: "ModelProject",
        meta: { title: "模型库", svgIcon: "on-model" },
        children: [
          {
            path: "/entity",
            name: "Entity",
            redirect: "/entity/index",
            children: [
              {
                path: "/entity/index",
                name: "EntityIndex",
                component: () => import("@/pages/entity/index.vue"),
                meta: {
                  title: "实体模型",
                  keepAlive: true,
                  component: "EntityIndex"
                }
              },
              {
                path: "/entity/create",
                name: "EntityCreate",
                component: () => import("@/pages/entity/create.vue"),
                meta: {
                  title: "创建实体",
                  hidden: true,
                  activeMenu: "/entity/index",
                  keepAlive: true,
                  component: "EntityCreate"
                }
              },
              {
                path: "/entity/view",
                name: "EntityView",
                component: () => import("@/pages/entity/detail.vue"),
                meta: {
                  title: "实体详情",
                  hidden: true,
                  activeMenu: "/entity/index",
                  keepAlive: true,
                  component: "EntityView"
                }
              }
            ]
          },
          // {
          //   path: "/resource/behavior",
          //   name: "Behavior",
          //   component: () => import("@/pages/behavior/index.vue"),
          //   meta: {
          //     title: "行为模型",
          //     keepAlive: true,
          //     component: "Behavior"
          //   }
          // },
          {
            path: "/component",
            name: "Component",
            meta: { title: "组件模型" },
            redirect: "/component/index",
            children: [
              {
                path: "/component/index",
                name: "ComponentIndex",
                component: () => import("@/pages/component/index.vue"),
                meta: {
                  title: "组件模型",
                  keepAlive: true,
                  component: "ComponentIndex"
                }
              },
              {
                path: "/component/create",
                name: "ComponentCreate",
                component: () => import("@/pages/component/create.vue"),
                meta: {
                  title: "创建组件模型",
                  hidden: true,
                  activeMenu: "/component/index",
                  keepAlive: true,
                  component: "ComponentCreate"
                }
              },
              {
                path: "/component/view",
                name: "ComponentView",
                component: () => import("@/pages/component/detail.vue"),
                meta: {
                  title: "组件模型详情",
                  hidden: true,
                  activeMenu: "/component/index",
                  keepAlive: true,
                  component: "ComponentView"
                }
              }
            ]
          },
          {
            path: "/basicData",
            name: "BasicData",
            redirect: "/basicData/index",
            meta: { hidden: true, title: "基础数据" },
            children: [
              {
                path: "/basicData/index",
                name: "BasicDataIndex",
                component: () => import("@/pages/basicData/index.vue"),
                meta: {
                  hidden: true,
                  title: "基础数据",
                  keepAlive: true,
                  component: "BasicDataIndex"
                }
              },
              {
                path: "/basicData/create",
                name: "BasicDataCreate",
                component: () => import("@/pages/basicData/create.vue"),
                meta: {
                  title: "创建基础数据",
                  hidden: true,
                  activeMenu: "/component/index",
                  keepAlive: true,
                  component: "BasicDataCreate"
                }
              },
              {
                path: "/basicData/detail",
                name: "BasicDataView",
                component: () => import("@/pages/basicData/detail.vue"),
                meta: {
                  title: "基础数据详情",
                  hidden: true,
                  activeMenu: "/component/index",
                  keepAlive: true,
                  component: "BasicDataView"
                }
              }
            ]
          },
          // {
          //   path: "/resource/eval-model",
          //   name: "EvalModel",
          //   component: () => import("@/pages/eval-model/index.vue"),
          //   meta: {
          //     title: "评估模型",
          //     keepAlive: true,
          //     component: "EvalModel"
          //   }
          // }
        ]
      },
      {
        path: "/resource/more",
        name: "ResourceMore",
        redirect: "/resource/more/three-model",
        meta: { title: "更多", elIcon: "MoreFilled" },
        children: [
          {
            path: "/resource/more/three-model",
            name: "ThreeModel",
            component: () => import("@/pages/three-model/index.vue"),
            meta: { title: "三维模型库", keepAlive: true, component: "ThreeModel" }
          },
          {
            path: "/resource/more/military",
            name: "Military",
            component: () => import("@/pages/military/index.vue"),
            meta: { title: "军标库", keepAlive: true, component: "Military" }
          },
          {
            path: "/resource/more/engine-library",
            name: "EngineLibrary",
            redirect: "/index",
            meta: { title: "引擎库", keepAlive: true, component: "EngineLibrary" },
            children: [
              {
                path: "/index",
                name: "EngineLibraryIndex",
                component: () => import("@/pages/engine-library/index.vue"),
                meta: { title: "引擎库", keepAlive: true, component: "EngineLibrary"}
              },
              {
                path: "/view",
                name: "EngineLibraryView",
                component: () => import("@/pages/engine-library/view.vue"),
                meta: { title: "查看引擎", svgIcon: "on-model", hidden: true, activeMenu: "/index", component: "EngineLibraryView" }
              }
            ]
          }
        ]
      }
    ]
  },
  {
    path: "/more",
    name: "More",
    component: Layouts,
    redirect: "/more/common-setting",
    meta: { title: "更多", elIcon: "MoreFilled" },
    children: [
      {
        path: "/more/system",
        name: "System",
        redirect: "/more/system/user/index",
        meta: { title: "系统管理", isInnerLink: true },
        children: [
          {
            path: "/more/system/user",
            name: "User",
            meta: { title: "用户管理", svgIcon: "yonghuguanli" as any },
            children: [
              {
                path: "/more/system/user/index",
                name: "SystemUser",
                component: () => import("@/pages/system/user/index.vue"),
                meta: { title: "用户管理", svgIcon: "yonghuguanli" as any, keepAlive: true, component: "SystemUser" }
              },
              {
                path: "/more/system/user/create",
                name: "SystemUserCreate",
                component: () => import("@/pages/system/user/create.vue"),
                meta: { title: "创建用户", hidden: true, activeMenu: "/more/system/user/index", keepAlive: true, component: "SystemUserCreate" }
              },
              {
                path: "/more/system/user/detail",
                name: "SystemUserDetail",
                component: () => import("@/pages/system/user/create.vue"),
                meta: { title: "用户详情", hidden: true, activeMenu: "/more/system/user/index", keepAlive: true, component: "SystemUserCreate" }
              }
            ]
          },
          {
            path: "/more/system/role",
            name: "SystemRole",
            component: () => import("@/pages/system/role/index.vue"),
            meta: { title: "角色管理", svgIcon: "jiaoseguanli", keepAlive: true, component: "SystemRole" }
          },
          {
            path: "/more/system/menu",
            name: "SystemMenu",
            component: () => import("@/pages/system/menu/index.vue"),
            meta: { title: "菜单管理", svgIcon: "caidanguanli", keepAlive: true, component: "SystemMenu" }
          }
        ]
      },
      // {
      //   path: "/more/help",
      //   name: "Help",
      //   component: () => import("@/pages/help/index.vue"),
      //   meta: { title: "帮助" }
      // }
    ]
  }
]

// 我的项目
export const ProjectRoutes: RouteRecordRaw[] = [
  {
    path: "/my-project",
    name: "MyProject",
    component: Layouts,
    redirect: "/my-project/index",
    meta: { hidden: true, isInnerLink: true },
    children: [
      {
        path: "/my-project/intelligent",
        name: "MyProjectIntelligent",
        redirect: "/my-project/intelligent/index",
        meta: { title: "智能体", svgIcon: "zhinengti", moduleName: "agent" },
        children: [
          {
            path: "/my-project/intelligent/index",
            name: "MyProjectIntelligentIndex",
            component: () => import("@/pages/intelligent/index.vue"),
            meta: { title: "智能体", svgIcon: "zhinengti", keepAlive: true, component: "IntelligentIndex" }
          },
          {
            path: "/my-project/intelligent/create",
            name: "MyProjectIntelligentCreate",
            component: () => import("@/pages/intelligent/create.vue"),
            meta: { title: "创建智能体", svgIcon: "on-model", hidden: true, activeMenu: "/my-project/intelligent/index", keepAlive: true, component: "IntelligentCreate" }
          },
          {
            path: "/my-project/intelligent/view",
            name: "MyProjectIntelligentView",
            component: () => import("@/pages/intelligent/view.vue"),
            meta: { title: "查看智能体", svgIcon: "on-model", hidden: true, activeMenu: "/my-project/intelligent/index", keepAlive: true, component: "IntelligentView" }
          }
        ]
      },
      {
        path: "/my-project/scn",
        name: "MyProjectScn",
        redirect: "/my-project/scn/index",
        meta: { title: "仿真想定", svgIcon: "xiangding", moduleName: "scn" },
        children: [
          {
            path: "/my-project/scn/index",
            name: "MyProjectScnIndex",
            component: () => import("@/pages/scn/index.vue"),
            meta: { title: "仿真想定", svgIcon: "xiangding", keepAlive: true, component: "ScnIndex" }
          },
          {
            path: "/my-project/scn/view",
            name: "MyProjectScnView",
            component: () => import("@/pages/scn/Edit/index.vue"),
            meta: { title: "查看仿真想定", svgIcon: "on-model", hidden: true, activeMenu: "/my-project/scn/index", keepAlive: true, component: "ScnView" }
          }
        ]
      },
      {
        path: "/my-project/train",
        name: "MyProjectTrain",
        redirect: "/my-project/train/index",
        meta: { title: "训练任务", svgIcon: "xunlian", moduleName: "train" },
        children: [
          {
            path: "/my-project/train/index",
            name: "MyProjectTrainIndex",
            component: () => import("@/pages/train/index.vue"),
            meta: { title: "训练任务", svgIcon: "xunlian", keepAlive: true, component: "TrainIndex" }
          },
          {
            path: "/my-project/train/create",
            name: "TrainCreate",
            component: () => import("@/pages/train/create.vue"),
            meta: { title: "创建训练任务", svgIcon: "on-model", hidden: true, activeMenu: "/my-project/train/index", keepAlive: true, component: "TrainCreate" }
          },
          {
            path: "/my-project/train/view",
            name: "MyProjectTrainView",
            component: () => import("@/pages/train/view.vue"),
            meta: { title: "查看训练任务", svgIcon: "on-model", hidden: true, activeMenu: "/my-project/train/index", keepAlive: true, component: "TrainView" }
          }
        ]
      },
      {
        path: "/my-project/deduct",
        name: "MyProjectDeduct",
        redirect: "/my-project/deduct/index",
        meta: { title: "推演任务", svgIcon: "tuiyan", moduleName: "inference" },
        children: [
          {
            path: "/my-project/deduct/index",
            name: "MyProjectDeductIndex",
            component: () => import("@/pages/deduct/index.vue"),
            meta: { title: "推演任务", svgIcon: "tuiyan", keepAlive: true, component: "DeductIndex" }
          },
          {
            path: "/my-project/deduct/createMode",
            name: "MyProjectDeductCreateMode",
            component: () => import("@/pages/deduct/components/create-mode/create-mode.vue"),
            meta: { title: "选择创建推演任务", svgIcon: "on-model", hidden: true, activeMenu: "/my-project/deduct/index", keepAlive: true, component: "DeductCreateMode" }
          },
          {
            path: "/my-project/deduct/create",
            name: "MyProjectDeductCreate",
            component: () => import("@/pages/deduct/create.vue"),
            meta: { title: "创建推演任务", svgIcon: "on-model", hidden: true, activeMenu: "/my-project/deduct/index", keepAlive: true, component: "DeductCreate" }
          },
          {
            path: "/my-project/deduct/view",
            name: "DeductView",
            component: () => import("@/pages/deduct/view.vue"),
            meta: { title: "查看推演任务", svgIcon: "on-model", hidden: true, activeMenu: "/my-project/deduct/index", keepAlive: true, component: "DeductView" }
          },
          {
            path: "/my-project/deduct/components/check",
            name: "DeductCheck",
            component: () => import("@/pages/deduct/components/check/index.vue"),
            meta: { title: "记录回放", svgIcon: "on-model", hidden: true, activeMenu: "/my-project/deduct/index", keepAlive: true, component: "DeductCheck" }
          }
        ]
      },
      // {
      //   path: "/my-project/evaluate",
      //   name: "Evaluate",
      //   redirect: "/my-project/evaluate/index",
      //   meta: { title: "评估任务", svgIcon: "pinggu", moduleName: "evaluate" },
      //   children: [
      //     {
      //       path: "/my-project/evaluate/index",
      //       name: "EvaluateIndex",
      //       component: () => import("@/pages/evaluate/index.vue"),
      //       meta: { title: "评估任务", svgIcon: "pinggu", keepAlive: true, component: "EvaluateIndex" }
      //     }
      //   ]
      // }
    ]
  }
]
dynamicRoutes.push(...ProjectRoutes)

export const ResourceRoutePath: { [propNmae: string]: string[] } = {}
export const RouteTitleMap: { [propNmae: string]: string } = {}
// 获取属于独立路由的所有项
collectInnerLinkRoutes(dynamicRoutes, ResourceRoutePath, RouteTitleMap, undefined)

/** 路由实例 */
export const router = createRouter({
  routes: routerConfig.thirdLevelRouteCache ? flatMultiLevelRoutes(constantRoutes) : constantRoutes,
  history: routerConfig.history
})

/** 重置路由 */
export function resetRouter() {
  try {
    // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
    router.getRoutes().forEach((route) => {
      const { name, meta } = route
      if (name && meta.roles?.length) {
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch {
    // 强制刷新浏览器也行，只是交互体验不是很好
    location.reload()
  }
}

// 注册路由导航守卫
registerNavigationGuard(router)

// {
//   path: "/intelligent/view/:id",
//   name: "IntelligentView",
//   component: () => import("@/pages/intelligent/view.vue"),
//   meta: { title: "查看智能体", svgIcon: "on-model", hidden: true, activeMenu: "/intelligent/index" },
//   props: route => ({
//     id: route.params.id
//   }),
//   beforeEnter: async (to, from, next) => {
//     const { data } = await getTaskInfoApi(to.params.id as string)
//     to.meta.title = `智能体 - ${data.task_name}`
//     next()
//   }
// }
